﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomerService
{
    public partial class AddDetailItem : System.Web.UI.Page
    {
        private SalesManagerEntities _sme;

        protected void Page_Load(object sender, EventArgs e)
        {
            _sme = (SalesManagerEntities)Session["sme"];
            if (!IsPostBack && !string.IsNullOrEmpty(Request.QueryString["OwnerID"]))
            {
                int ownerID = int.Parse(Request.QueryString["OwnerID"]);
                ListView1.DataSource = (from c in _sme.tblProduct.Include("ProductPricing") where c.ProductGroup.OwnerID == ownerID && c.SalableItem == true orderby c.ItemCode select c).ToArray();
                ListView1.DataBind();
            }
        }

        protected void btnSelect_Click(object sender, CommandEventArgs e)
        {
            int ownerID = int.Parse(Request.QueryString["OwnerID"]);
            int key = int.Parse(e.CommandArgument.ToString());

            tblProduct product = _sme.tblProduct.Find(new object[] {key});

            switch (Request.QueryString["Source"])
            {
                case "OrderDetail":
                    {
                        tblOrderHeader oh = _sme.tblOrderHeader.Find(new object[] { int.Parse(Request.QueryString["OrderID"]) });

                        tblOrderDetail newDetail = new tblOrderDetail();
                        newDetail.OrderHeader = oh;
                        newDetail.ImportDate = DateTime.Today;
                        newDetail.Product = product;
                        newDetail.Qty = 1;
                        newDetail.UnitPrice = product.DefaultPrice;
                        newDetail.Discount = 0;
                        _sme.tblOrderDetail.Add(newDetail);
                        Session["sme"] = _sme;

                        // Trigger the OrderDetail panel to refresh.            
                        if (!Page.ClientScript.IsStartupScriptRegistered("PostBack"))
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "PostBack",
                                "<script  language='javascript' type='text/javascript'>" +
                                "opener.__doPostBack('" + Request.QueryString["PanelID"] + "', 'UpdateDetails'); window.close('fred');" +
                                "</script>"
                                );
                    }
                    break;
                case "Sequence":
                    {
                        int customerID = int.Parse(Request.QueryString["CustomerID"]);
                        tblCustomer customer = _sme.tblCustomer.Find(new object[] { customerID });

                        // Find a default account to use.
                        tblAccount acc = null;
                        foreach(tblOrderHeader oh in customer.OrderHeaders.OrderByDescending(c=>c.OrderID))
                            if (oh.tblAccount != null)
                            {
                                acc = oh.tblAccount;
                                break;
                            }

                        tblSequenceOrder newSequenceOrder = new tblSequenceOrder();
                        newSequenceOrder.Account = acc;
                        newSequenceOrder.CreateDate = DateTime.Now;
                        newSequenceOrder.CustomerID = customerID;
                        newSequenceOrder.SequenceStatus = _sme.codeSequenceStatus.Find(new object[] { "A" });
                        newSequenceOrder.OwnerID = ownerID;
                        newSequenceOrder.ShipTo_AddressID = customer.Person.Address.AddressID;
                        newSequenceOrder.ShipTo_PersonID = customer.Person.PersonID;
                        _sme.tblSequenceOrder.Add(newSequenceOrder);

                        tblSequenceOrderStep newSequenceOrderStep = new tblSequenceOrderStep();
                        newSequenceOrderStep.CurrentCount = 0;
                        newSequenceOrderStep.Delay = 0;
                        newSequenceOrderStep.NextTriggerDate = DateTime.Today.AddDays(1);
                        newSequenceOrderStep.RecurrenceCount = 0;
                        newSequenceOrderStep.RecurrenceTerm = 0;
                        newSequenceOrderStep.SequenceOrder = newSequenceOrder;
                        newSequenceOrderStep.Shipping = 0;
                        _sme.tblSequenceOrderStep.Add(newSequenceOrderStep);

                        tblSequenceOrderStepProduct sosp = new tblSequenceOrderStepProduct();
                        sosp.Discount = 0;
                        sosp.Product = product;
                        sosp.Qty = 1;
                        sosp.SequenceOrderStep = newSequenceOrderStep;
                        sosp.UnitPrice = product.DefaultPrice;
                        _sme.tblSequenceOrderStepProduct.Add(sosp);

                        // Trigger the Sequences panel to refresh.            
                        if (!Page.ClientScript.IsStartupScriptRegistered("PostBack"))
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "PostBack",
                                "<script  language='javascript' type='text/javascript'>" +
                                "opener.__doPostBack('" + Request.QueryString["PanelID"] + "', 'UpdateDetails'); window.close('fred');" +
                                "</script>"
                                );
                    }
                    break;
                case "SequenceStep":
                    {
                        int sequenceOrderStepID = int.Parse(Request.QueryString["SequenceOrderStepID"]);
                        tblSequenceOrderStep sequenceOrderStep = _sme.tblSequenceOrderStep.Find(new object[] { sequenceOrderStepID });

                        tblSequenceOrderStepProduct sosp = new tblSequenceOrderStepProduct();
                        sosp.Discount = 0;
                        sosp.Product = product;
                        sosp.Qty = 1;
                        sosp.SequenceOrderStep = sequenceOrderStep;
                        sosp.UnitPrice = product.DefaultPrice;
                        _sme.tblSequenceOrderStepProduct.Add(sosp);

                        // Trigger the Sequences panel to refresh.            
                        if (!Page.ClientScript.IsStartupScriptRegistered("PostBack"))
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "PostBack",
                                "<script  language='javascript' type='text/javascript'>" +
                                "opener.__doPostBack('" + Request.QueryString["PanelID"] + "', 'UpdateDetails'); window.close('fred');" +
                                "</script>"
                                );
                    }
                    break;
            }
        }

        protected void ListView1_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
        {
        }
    }
}
